import { crtChannelKs } from "./crtChannelKs";
import crtkc from "./crtkc";


/**快救救孩子吧 */
export class crtChannelKs1 extends crtChannelKs {
    channelObj: any = (window as any).ks;
    paraMap: Map<String, string> = new Map([
        ['appId', 'ks653866374863418920'],
        ['vedioAd', '2300015623_01'],
        ['insertAd', ''],
        ['bannerAd', ''],
        ['ctrlId', '676cd830ac07a817fa283c6c'], //策略开头id
        ['gid', ''], //gid
    ]);
    /** 游戏名称，用于远程存档,隐私政策等显示用 */
    gameName: string = '快救救孩子吧';

    ruanzhu: string = '著作权人：南京天豆互动网络科技有限公司\n登记号：2021SR1411702';

    /** 分渠道的广告特定动作，actId区分哪个动作,para为执行动作需要的对象
     * 1:点击开始游戏, 恋爱的如花 如花与石头弹出视频广告
     */

    doAction(actId: number, para: any) {
        let watchAd = ()=>{
            this.showVideo(()=>{

            });
        }
        if(para && para[0] == 'ks1'){
            if(actId == 1){
                let isOpen = this.getCtrl('p2');
                if(isOpen){
                    para[1].active = true;
                    para[2].active = true;
                }else{
                    para[1].active = false;
                    para[2].active = false;
                }
            }else if(actId == 2){
                para[1].active = true;
                let isOpen = this.getCtrl('p3');
                if(isOpen){
                    para[2].active = true;
                    para[3].active = false;
                }else{
                    para[2].active = false;
                    para[3].active = true;
                }
            }else if(actId == 3){
                this.showVideo(()=>{
                    para[1] && para[1]();
                });
            }
        }
    }

    bannerAd: any = null;
    showBanner(isForce?: boolean, styleFn?: any, where?: string): void {
        return;
        const id = this.getPara('bannerAd');
        if (!id) {
            return;
        }
        crtkc.log('showBanner isShow', id, where);
        if (this.bannerAd) {
            return;
        }
        const ks = this.channelObj;
        const conf = {
            'adUnitId': id,
            adIntervals: 30,
            style: {
                left: 0,
                top: this.getSysInfo().windowHeight / 2,
                width: 200,
                height: 200
            },
        }
        this.bannerAd = ks.createBannerAd(conf);
        this.bannerAd.onError(err => {
            crtkc.log('banner广告加载失败' + JSON.stringify(err));
            this.bannerAd = null;
        });

        this.bannerAd.onResize((size) => {
            console.log("banner onResize===", size.height, size.width);
            const style = (styleFn) ? styleFn(size) : { top: this.getSysInfo().windowHeight - size.height, left: (this.getSysInfo().windowWidth - size.width) / 2 };
            this.bannerAd.style.top = style.top;
            this.bannerAd.style.left = style.left;
            if (style.height) {
                this.bannerAd.style.height = style.height;
            }
            crtkc.log('bannerAd style:', this.bannerAd.style.top, this.bannerAd.style.left, this.bannerAd.style.height);
        });

        this.bannerAd.show().then(() => {
            crtkc.log('banner显示成功');
        }).catch(err => {
            this.bannerAd = null;
            crtkc.log('showBanner err:' + JSON.stringify(err));
        });
    }

    showVideo(succ): void {
        if(!this.getCtrl('p1')){
            succ && succ();
            return;
        }
        const id = this.getPara('vedioAd');
        console.error('ks showVideo');
        if (!id) {
            return;
        }
        
        cc.game.pause();
        this.rewardedVideoAd = this.channelObj.createRewardedVideoAd({
            adUnitId: id
        });
        if (this.videoFns['onLoad']) {
            this.rewardedVideoAd.offLoad(this.videoFns['onLoad']);
        }
        if (this.videoFns['onError']) {
            this.rewardedVideoAd.offError(this.videoFns['onError']);
        }
        if (this.videoFns['onClose']) {
            this.rewardedVideoAd.offClose(this.videoFns['onClose']);
        }
        this.rewardedVideoAd.offLoad();
        this.rewardedVideoAd.offError();
        this.rewardedVideoAd.offClose();
        const me = this;
        me.videoFns['onLoad'] = () => {
            crtkc.log('激励视频 广告加载成功');
        };
        me.videoFns['onError'] = (err) => {
            crtkc.err(err);
            cc.game.resume();
            // 视频失败切换分享
            this.channelObj.showToast({
                title: '暂无广告',
            });
        }
        me.videoFns['onClose'] = (res) => {
            // 用户点击了【关闭广告】按钮
            // 小于 2.1.0 的基础库版本，res 是一个 undefined
            cc.game.resume();
            if (res && res.isEnded) {
                // 正常播放结束，可以下发游戏奖励
                succ && succ();
            } else {
            }
        };
        me.rewardedVideoAd.onLoad(me.videoFns['onLoad']);
        me.rewardedVideoAd.onError(me.videoFns['onError']);
        me.rewardedVideoAd.onClose(me.videoFns['onClose']);

        me.rewardedVideoAd.show()
            .then(() => {

            }).catch(err => {
                me.rewardedVideoAd.load().then(() => {
                    // 加载成功后需要再显示广告
                    return me.rewardedVideoAd.show().then(() => {
                        
                    }).catch((err) => {
                        cc.game.resume();
                    });
                }).catch((err) => {
                    cc.game.resume();
                });
            });

    }

    doShowInter(isForce?: boolean,  style?: any): void {
        return;
        const id = this.getPara('insertAd');
        if (!id) {
            return;
        }
        console.error("doShowInter: ", this.nextInterTime, Date.now());
        if (this.nextInterTime > Date.now()) {
            crtkc.err('插屏时间间隔未到');
            return;
        }
        let interstitialAd = this.channelObj.createInterstitialAd({
            adUnitId: id,
        });  
        if (interstitialAd) {  
            interstitialAd.onClose(res => {  
                // 插屏广告关闭事件  
                crtkc.err("插屏广告 onClose: ", res);
            })  
            interstitialAd.onError(res => {  
                // 插屏广告Error事件  
                crtkc.err("插屏广告 onError: ", res);
            })  
            let p = interstitialAd.show()  
            p.then(function(result){  
                // 插屏广告展示成功  
                console.error(`show interstitial ad success, result is ${result}`);
                this.nextInterTime = Date.now() + this.interGap;  
            }).catch(function(error){  
                // 插屏广告展示失败  
                console.error('show interstitial ad failed, error is: ', error)  
                if (error.code === -10005) {  
                    // 表明当前app版本不支持插屏广告，可以提醒用户升级app版本 
                    console.error("不支持插屏广告"); 
                }  
            })  
        } else {  
               console.log("创建插屏广告组件失败");  
        }  
    }


    showInter(isBanner: boolean = false, style?: any) {
        return;
        const id = this.getPara('insertAd');
        if (!id) {
            return;
        }
        // 插屏广告弹出页面
        // 0为关闭，1为开启
        if (!this.getCtrl('p4')) {
            return;
        }
        
        //原生插屏延迟
        const p5 = this.getCtrl('p5');
        const me = this;
        setTimeout(() => {
            me.doShowInter(isBanner, style);
        }, p5 * 1000);
    }



}